﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace huaguwang.hgmanage.Power
{
    public partial class PowerGroup : PageBaseAdmin
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            Pager.PageSize = 10;
            //用来初始化用户的按钮,执行此语句的时候,but并没有具体产生
            WebUserToolBar1.MainInit(
              UserConst.Exp_GetUser,
              UserConst.Exp_GetButtonList,
              (i, but) =>
              {
                  switch (i)
                  {
                      case 1:
                          but.Attributes.Add("onclick", "return GetUpdateCheckDefault();");
                          break;
                      case 2:
                          but.Attributes.Add("onclick", "return GetDelCheckDefault();");
                          break;
                      case 3:
                          but.Attributes.Add("onclick", "return GetUpdateCheckDefault();");
                          break;
                      default:
                          break;
                  }
              },
             UserConst.Exp_ToolBarErr,
              new huaguwang.WebControls.WebUserToolBar.SelectItem()
              {
                  FuncGetText = () => TextBox1.Text.Trim(),
                  FuncSetText = v => { TextBox1.Text = v; },
                  Name = TextBox1.ID
              }
           );

            if (!IsPostBack)
            {
                //分页查询
                SetData(0);

            }

        }
        readonly D_T_PowerGroup_Exp _Dal = new D_T_PowerGroup_Exp();

        #region 基本的带有查询的事件组合
        /// <summary>
        /// 分页控件的页面事件
        /// </summary>
        /// <param name="src"></param>
        /// <param name="e"></param>
        protected void Pager_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            SetData((e.NewPageIndex - 1) * Pager.PageSize);
        }
        /// <summary>
        /// 查询的实现方法
        /// </summary>
        /// <param name="begin"></param>
        public void SetData(int begin)
        {
            var txtPName = this.WebUserToolBar1.MainGetQuery(this.TextBox1.ID);
            Pager.RecordCount = _Dal.GetListPage(txtPName);
            this.Repeater1.DataSource = _Dal.GetListPage(begin, Pager.PageSize, txtPName);
            this.Repeater1.DataBind();
        }
        /// <summary>
        ///  权限详细按钮事件
        /// </summary>
        protected void WebUserToolBar1_Button3(object sender, EventArgs e)
        {
            var strID = Request.Params["HiddenID"].Exp_Trim();
            var sKey = this.WebUserToolBar1.MainSetSKey();
            Response.Redirect("PowerGroup_ESon.aspx?ID=" + strID + "&" + sKey, false);
        }
        /// <summary>
        /// 增加按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button0(object sender, EventArgs e)
        {
            var sKey = this.WebUserToolBar1.MainSetSKey();
            Response.Redirect("PowerGroup_E.aspx?" + sKey, false);
        }

        /// <summary>
        /// 修改按钮的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button1(object sender, EventArgs e)
        {
            var strID = Request.Params["HiddenID"].Exp_Trim();
            var sKey = this.WebUserToolBar1.MainSetSKey();
            Response.Redirect("PowerGroup_E.aspx?ID=" + strID + "&" + sKey, false);
        }
        /// <summary>
        /// 删除按钮的事件
        /// </summary>
        protected void WebUserToolBar1_Button2(object sender, EventArgs e)
        {
            var strID = Request.Params["HiddenID"].Exp_Trim();
            var intA = strID.Exp_SplitToList(',', Sel => Sel.Exp_IntTryParse()).Where(Sel => Sel > 0).ToList();
            if (intA == null || intA.Count <= 0)
            {
                this.Exp_PageAlter("没有找到可以删除的信息");
                return;
            }

            int i = 0;
            foreach (var item in intA)
            {
                i++;
                //检测权限
                var tempI = _Dal.IsUserCoun(item);
                if (!tempI)
                {
                    this.Exp_PageAlter("当前第" + i + "条资料中还包含有用户,不能够直接删除");
                    return;
                }

            }


            var refInt = _Dal.UpdateFlag(intA);
            if (refInt > 0)
            {
                //this.Exp_PageWriteLog("资料删除成功",
                //    "删除成功,删除的id:{0},共{1}个,成功删除{2}个".Exp_Format(strID, intA.Count, refInt),
                //    "删除按钮资料",
                //    true);
                SetData(0);
                this.Exp_PageAlter("删除操作成功");
                return;
            }
            else
            {
                this.Exp_PageAlter("删除操作失败");
                return;
            }
            //this.Exp_PageAlter("获得了" + strID);
        }
        #endregion

        protected void WebUserToolBar1_Button4(object sender, EventArgs e)
        {
            WebUserToolBar1.MainSetQuery();
            SetData(0);
        }
    }
}
